package gom.example.gramediamajalah.json;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class JSONParser {

	public JSONArray AmbilJson(String url) {

		InputStream isi = null;
		JSONArray jsonarray = null;
		String json = "";

		try {
			 HttpClient klien = new DefaultHttpClient();
			HttpGet get = new HttpGet(url);

			 HttpResponse respon = klien.execute(get);
			 isi = respon.getEntity().getContent();

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					isi), 4096);
			 String baris =null;
			 StringBuilder sb = new StringBuilder();
			 try {
				 while ((baris=rd.readLine())!=null){
					 sb.append(baris);
				 }
				 isi.close();
			 }catch (Exception e) {
				 Log.d("Error String",e.toString()) ;
			 }
			 json=sb.toString();
//		
		} catch (Exception e) {
			Log.e("Buffer Error", "Error converting result " + e.toString());
		}

		try {
			jsonarray= new JSONArray(json);
		} catch (JSONException e) {
			Log.e("JSON Parser", "Error parsing data " + e.toString());
		}

		return jsonarray;

	}
	
}